<template>
	<view>
		<view class="nav2">
			<u-navbar title="订单列表" bgColor="transparent" :fixed="false" autoBack>
			</u-navbar>
		</view>
		<view class="m30">
			<view class="flex-jus">
				<view v-for="(item,index) in navList" :key="index" class="c999 f28"
					:class="{'active pore':navIndex == index}" @click="navClick(index)">
					{{item.name}}
				</view>
			</view>
			<u-gap height="20"></u-gap>
			<view v-if="orderList.length">
				<view class="bgFff radius30 p30 mb20" v-for="(item,index) in orderList" :key="index">
					<view class="flex-jus f26">
						<text class="c999">订单号：{{item.orderCode}}</text>
						<text class="blue" v-if="item.orderStatus == 0">待付款</text>
						<text class="c343" v-if="item.orderStatus == 1 && item.orderType!=3">待出发</text>
						<text class="c343" v-if="item.orderStatus == 1 && item.orderType==3">待使用</text>
						<text class="green" v-if="item.orderStatus == 2">待完成</text>
						<text class="c343" v-if="item.orderStatus == 3">待确认</text>
						<text class="c343" v-if="item.orderStatus == 4">已完成</text>
						<text class="c343" v-if="item.orderStatus == 11">已取消</text>
					</view>
					<u-gap height="10"></u-gap>
					<view v-if="item.orderType==3" @click="openUrl('/subpage/order/tuangouDetail?id='+item.id)">
						<view class="flex flex-start">
							<u-image :src="api_host + item.orderPro.icon" mode="" width="76px" height="76px"
								radius="5"></u-image>
							<view class="p10 ml10 flex1">
								<view class="">
									<view class="c343 f28 bold">{{item.orderPro.proName}}</view>
									<!-- <view class="f24 c999">￥ {{item.orderPrice}}</view> -->
								</view>
							</view>
						</view>
					</view>
					<view v-else @click="openUrl('/subpage/order/detail?id='+item.id)">
						<view class="flex flex-start">
							<u-image :src="api_host + item.orderPro.icon" mode="" width="76px" height="76px"
								radius="5"></u-image>
							<view class="p10 ml10 flex1">
								<view class="c343 f28 bold">{{item.orderPro.proName}}</view>
								<u-gap height="10"></u-gap>
								<view>
									<view class="flex-jus f24 c999 mb10" v-for="(item2,index2) in item.orderDetails"
										:key="index2">
										<text>{{item2.name}} x{{item2.buyCount}}</text>
										<text>¥ {{item2.price}}</text>
									</view>
								</view>
							</view>
						</view>
					</view>
					<u-gap height="10" v-if="item.orderStatus <= 3"></u-gap>
					<view class="flex-jus" v-if="item.orderStatus <= 3">
						<view class="c999 f24">
							<text>总价 ¥ {{item.orderPrice + (item.discountPrice?Number(item.discountPrice):0)}}</text>
							<text class="ml20" v-if="item.coupId">优惠 ¥ {{item.discountPrice || '0.00'}}</text>
						</view>
						<view class="flex">
							<text class="c343 f26">{{item.orderStatus > 0 && item.orderStatus != 11?'实付款':'应付款'}}</text>
							<view class="cyellow ml10">
								<text class="f24">￥</text>
								<text class="f28 bold">{{item.orderPrice}}</text>
							</view>
						</view>
					</view>
					<u-line color="#eee" margin="20rpx 0"></u-line>
					<view v-if="item.orderAppend" class="mb20" @click="openUrl('/subpage/order/detail?id='+item.id)">
						<view class="bgF7 p20 radiusBox mb20">
							<view class="c343 f26 mb20 bold">追加服务</view>
							<view class="f24 c343 mt10">
								{{item.orderAppend.describe}}
							</view>
							<!-- <view class="flex" v-if="item.sceneImages">
								<view class="m10" v-for="(img,imgIndex) in item.sceneImages.split(',')" :key="index">
									<u-image :src="api_host + img" mode="" width="76px" height="76px"
										radius="5"></u-image>
								</view>
							</view> -->
						</view>
						<view class="jus-right flex" v-if="item.orderStatus <= 3">
							<text class="c343 f26">{{item.orderStatus > 3 && item.orderStatus != 11?'实付款':'应付款'}}</text>
							<view class="cyellow ml10">
								<text class="f24">￥</text>
								<text class="f28 bold">{{item.orderAppend.price}}</text>
							</view>
						</view>
					</view>
					<view class="flex-jus" v-if="item.orderStatus > 3 && item.orderStatus != 11">
						<view class="c999 f24">
							<text>总价
								¥{{Number(item.orderPrice + (item.discountPrice?Number(item.discountPrice):0) + (item.orderAppend?Number(item.orderAppend.price):0)).toFixed(2)}}</text>
							<text class="ml20" v-if="item.coupId">优惠 ¥ {{item.discountPrice || '0.00'}}</text>
						</view>
						<view class="flex">
							<text class="c343 f26">{{item.orderStatus > 3 && item.orderStatus != 11?'实付款':'应付款'}}</text>
							<view class="cyellow ml10">
								<text class="f24">￥</text>
								<text
									class="f28 bold">{{item.orderPrice + (item.orderAppend?Number(item.orderAppend.price):0)}}</text>
							</view>
						</view>
					</view>
					<u-line color="#eee" margin="20rpx 0"
						v-if="item.orderStatus > 3 && item.orderStatus != 11"></u-line>
					<view class="flex-jus">
						<view v-if="item.providerPhone">
							<u-button shape="circle" color="#EDECEF" @click="maketel(item.providerPhone)">
								<image src="../../static/images/order/tel.png" mode=""
									style="width: 40rpx;height: 40rpx;"></image>
							</u-button>
						</view>
						<view v-else>
							<u-button shape="circle" color="#EDECEF" @click="maketel(config.phone)">
								<image src="../../static/images/order/tel.png" mode=""
									style="width: 40rpx;height: 40rpx;"></image>
							</u-button>
						</view>
						<view class="flex" v-if="item.orderStatus < 1">
							<view>
								<u-button shape="circle" color="#fff" @click="cancelOrderbtn(item)"
									:customStyle="{'color':'#34314F','border':'1px solid #ddd','height':'70rpx','fontSize':'24rpx'}">
									取消订单
								</u-button>
							</view>
							<view class="ml20" v-if="item.orderType!=3">
								<u-button shape="circle" color="#fff"
									@click="openUrl('/subpage/me/addressList?type=order&order_id='+item.id)"
									:customStyle="{'color':'#34314F','border':'1px solid #ddd','height':'70rpx','fontSize':'24rpx'}">
									修改地址
								</u-button>
							</view>
							<view v-if="item.orderStatus == 0" class="ml20">
								<u-button shape="circle" color="#FFDE6D" @click="payOrder(item)"
									:customStyle="{'color':'#34314F','border':'1px solid #FFDE6D','height':'70rpx','fontSize':'24rpx'}">
									支付
								</u-button>
							</view>
						</view>
						<view class="textR" v-if="item.orderStatus == 1 && item.orderType!=3">
							<view class="f26 c343" v-if="item.estimateTime">
								<view>预计上门时间:{{item.estimateTime}}</view>
							</view>
							<view v-else class="f26 c343">
								平台正在分配工作人员
							</view>
						</view>
						<view class="textR" v-if="item.orderStatus == 2">
							<view class="f24 c999">工作人员已出发</view>
							<view class="f26 c343 mt10">{{item.estimateTime?('预计上门时间:'+item.estimateTime):'等待服务'}}
							</view>
						</view>
						<view v-if="item.orderStatus == 3">
							<!-- <u-button shape="circle" color="#FFDE6D" @click="payOrderappend(item)"
								v-if="item.orderAppend"
								:customStyle="{'color':'#34314F','border':'1px solid #FFDE6D','height':'70rpx','fontSize':'24rpx'}">
								支付追加服务
							</u-button>
							<u-button shape="circle" color="#FFDE6D" @click="advanceStatusbtn(item)" v-else
								:customStyle="{'color':'#34314F','border':'1px solid #FFDE6D','height':'70rpx','fontSize':'24rpx'}">
								确认成果
							</u-button> -->
							<u-button shape="circle" color="#FFDE6D"
								@click="openUrl('/subpage/order/serviceResult?id='+item.id)"
								:customStyle="{'color':'#34314F','border':'1px solid #FFDE6D','height':'70rpx','fontSize':'24rpx'}">
								确认成果
							</u-button>
						</view>
						<view class="flex" v-if="item.orderStatus == 4">
							<view v-if="item.orderType!=3">
								<u-button shape="circle" color="#fff"
									@click="openUrl('/subpage/order/serviceResult?id='+item.id)"
									:customStyle="{'color':'#34314F','border':'1px solid #ddd','height':'70rpx','fontSize':'24rpx'}">
									查看服务成果
								</u-button>
							</view>
							<view v-if="item.isComm == 0 && (item.orderType == 1 || item.orderType==3) && item.orderStatus === 4" class="ml20">
								<u-button shape="circle" color="#FFDE6D"
									@click="openUrl('/subpage/order/evaluate?id='+item.id)"
									:customStyle="{'color':'#34314F','border':'1px solid #FFDE6D','height':'70rpx','fontSize':'24rpx'}">
									去评价
								</u-button>
							</view>
						</view>
						<view v-if="item.orderStatus == 11">
							<u-button v-if="item.orderType==3" shape="circle" color="#FFDE6D"
								:customStyle="{'color':'#34314F','border':'1px solid #FFDE6D','height':'70rpx','fontSize':'24rpx'}"
								@click="openUrl('/groupPurchase/detail/index?id=' + item.proId)">再次购买</u-button>
							<u-button v-else shape="circle" color="#FFDE6D"
								@click="item.orderType == 1?openUrl('/subpage/housekeeping/detail?id=' + item.orderPro.id):openUrl('/subpage/lock/index?id=' + item.orderPro.id)"
								:customStyle="{'color':'#34314F','border':'1px solid #FFDE6D','height':'70rpx','fontSize':'24rpx'}">
								再次购买
							</u-button>
						</view>
						<view v-if="item.orderStatus == 4 && item.isComm === 1">
							<u-button shape="circle" color="#FFDE6D"
								:customStyle="{'color':'#34314F','border':'1px solid #FFDE6D','height':'70rpx','fontSize':'24rpx'}"
								@click="openUrl('/groupPurchase/detail/index?id=' + item.proId)">再次购买</u-button>
							
						</view>
					</view>
					<view class="textR flex-jus mt20" v-if="item.orderStatus == 0">
						<text></text>
						<view class="flex">
							<image src="../../static/images/order/clock.png" mode=""
								style="width: 30rpx;height: 30rpx;"></image>
							<text class="f24 c999 ml10 mr10">剩余支付时间</text>
							<view class="ml10 order_time">
								<u-count-down :time="item.downTimes" format="HH:mm:ss"
									@finish="(val) =>{timeEnd(val,item)}"></u-count-down>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view v-else class="no-data">
				<text>暂无订单数据</text>
			</view>
		</view>
		<u-gap height="20"></u-gap>
	</view>
</template>

<script>
	import appInfo from '@/common/appInfo.js'
	import {
		getUserOrders,
		cancelOrder,
		advanceStatus,
		payOrderappend,
		payOrder
	} from '@/common/api_order.js'
	import formItem from '../../uni_modules/uview-ui/libs/config/props/formItem'
	export default {
		name: '',
		data() {
			return {
				navList: [{
						name: '全部',
						value: '0'
					},
					{
						name: '待付款',
						value: '1'
					},
					{
						name: '待完成',
						value: '2'
					},
					{
						name: '已完成',
						value: '3'
					},
					{
						name: '已取消',
						value: '4'
					},
				],
				orderList: [],
				navIndex: 0,
				pageNum: 1,
				lastPage: false
			}
		},
		onLoad(option) {

		},
		onShow() {
			this.orderList = []
			this.getUserOrders()
		},
		onReachBottom() {
			console.log("到底了");
			if (this.lastPage) {
				this.pageNum += 1
				this.getUserOrders()
			}
		},
		methods: {
			navClick(index) {
				if (this.navIndex == index) {
					return false
				}
				this.navIndex = index
				this.orderList = []
				this.pageNum = 1
				this.getUserOrders()
			},
			timeEnd(val, item) {
				item.orderStatus = 11
			},
			async getUserOrders() { //订单列表
				uni.showLoading({
					mask: true
				})
				const res = await getUserOrders({
					type: this.navList[this.navIndex].value,
					pageSize: 5,
					pageNum: this.pageNum
				})
				if (res.code == 200) {
					this.lastPage = res.lastPage
					this.orderList = this.orderList.concat(res.data)
					let tempObject = {}
					this.orderList = this.orderList.reduce((item, next) => {
						// item为没有重复id的数组，next为当前对象
						tempObject[next.id] ? '' : (tempObject[next.id] = true && item.push(next));
						return item;
					}, [])
					console.log(this.orderList, '88888');
					for (var i = 0; i < this.orderList.length; i++) {
						let item = this.orderList[i]
						if (item.orderStatus == 0) {
							let nowTimes = new Date().getTime()
							let createTime = item.createTime.replace(/-/g, "/")
							let orderTimes = new Date(createTime).getTime()
							let times30 = 30 * 60 * 1000
							item.downTimes = orderTimes + times30 - nowTimes
						}
					}
				}
				uni.hideLoading()
			},
			async cancelOrderbtn(item) {
				const that = this
				const {
					id
				} = item
				uni.showModal({
					title: '确定取消订单吗？',
					confirmText: '确定',
					success(res) {
						if (res.confirm) {
							that.cancelOrder(id)
						}
					}
				})
			},
			async cancelOrder(id) {
				uni.showLoading({
					mask: true
				})
				const res = await cancelOrder({
					id: id
				})
				if (res.code == 200) {
					// uni.reLaunch({
					// 	url: '/subpage/order/index'
					// })
					uni.redirectTo({
						url: '/subpage/order/index'
					})
					uni.showToast({
						title: "取消订单成功",
						icon: 'none'
					})
					this.getUserOrders()
				}
				uni.hideLoading()
			},
			async advanceStatusbtn(item) {
				const that = this
				const {
					id
				} = item
				uni.showModal({
					title: '确定完成订单吗？',
					confirmText: '确定',
					success(res) {
						if (res.confirm) {
							that.advanceStatus(id)
						}
					}
				})
			},
			async advanceStatus(id) {
				uni.showLoading({
					mask: true
				})
				const res = await advanceStatus({
					id: id
				})
				if (res.code == 200) {
					this.getUserOrders()
				}
				uni.hideLoading()
			},
			async payOrderappend(item) {
				uni.showLoading({
					mask: true
				})
				const res = await payOrderappend({
					code: item.orderAppend.appendCode
				})
				if (res.code == 200) {
					let config = res.data
					uni.requestPayment({
						provider: 'wxpay',
						timeStamp: config.timeStamp.toString(),
						nonceStr: config.nonceStr,
						package: 'prepay_id=' + config.package.prepay_id,
						signType: config.signType,
						paySign: config.paySign,
						success() {
							uni.hideLoading()
							uni.showToast({
								title: '支付成功',
								icon: 'success'
							})
							setTimeout(() => {
								that.getUserOrders()
							}, 800)
						},
						fail() {
							uni.hideLoading()
							uni.showToast({
								title: '支付失败',
								icon: 'error'
							})
						}
					})
				}
				uni.hideLoading()
			},
			async payOrder(item) {
				const that = this
				// #ifdef MP-WEIXIN
				let messageRes = await this.$utils.messageSubscriptionAuthorization(appInfo.tmplIds.order);
				// #endif
				uni.showLoading({
					mask: true,
					title: '正在支付中...'
				})
				const res = await payOrder({
					id: item.id
				})
				if (res.code == 200) {
					let config = res.data
					uni.requestPayment({
						provider: 'wxpay',
						timeStamp: config.timeStamp.toString(),
						nonceStr: config.nonceStr,
						package: 'prepay_id=' + config.package.prepay_id,
						signType: config.signType,
						paySign: config.paySign,
						success() {
							uni.hideLoading()
							uni.showToast({
								title: '支付成功',
								icon: 'success'
							})
							setTimeout(() => {
								that.getUserOrders()
							}, 800)
						},
						fail() {
							uni.hideLoading()
							uni.showToast({
								title: '支付失败',
								icon: 'error'
							})
						}
					})
				}
				uni.hideLoading()

			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #FEF4E8;
	}

	.active {
		color: #34314F;
		font-weight: bold;
		z-index: 10;
	}

	.active::after {
		position: absolute;
		content: '';
		left: -20rpx;
		top: 0;
		height: 100%;
		width: 70rpx;
		border-radius: 30rpx;
		background-color: #FEF4E8;
		background-image: linear-gradient(to right, #FFDE6D, #FFF1A6);
		z-index: -1;
	}
	.no-data {
		position: absolute;
		top: 40%;
		left: 50%;
		transform: translate(-50%, -50%);
		margin: 0;
	}
</style>